package com.yh.correct.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.yh.correct.model.JudicialDeptment;
import com.yh.correct.model.Page;
import com.yh.correct.model.ResultData;
import com.yh.correct.service.IJudicialDeptmentService;
/**
 * 工作管理模块的司法所控制层
 * @author Administrator
 */
@RestController
@RequestMapping("/judicialDeptment")
public class JudicialDeptmentController {
	private static Logger logger = LogManager.getLogger(JudicialDeptmentController.class);
	
	@Autowired
	private IJudicialDeptmentService judicialDeptmentService;
	/**
	 * 新增司法所
	 * @param judicialDeptment
	 * @return
	 */
	@RequestMapping("/add")
	public ResultData addJudicialDeptment(@Valid JudicialDeptment judicialDeptment){ 
		logger.info("添加矫正单位"+judicialDeptment);
		judicialDeptmentService.addJudicialDeptment(judicialDeptment);
		ResultData rd=new ResultData();
		rd.setCode(0);
		rd.setMessage("增加成功！");
		return rd;
	}
	
	/**
	 * 删除司法所
	 * @param judicialDeptment
	 * @return
	 */
	@RequestMapping("/del")
	public ResultData delJudicialDeptment(JudicialDeptment judicialDeptment){ 
		logger.info("删除矫正单位"+judicialDeptment);
		int i=judicialDeptmentService.delJudicialDeptment(judicialDeptment);
		ResultData rd=new ResultData();
		if(i>0){
			rd.setCode(0);
			rd.setMessage("删除成功！");
		}else{
			rd.setCode(-1);
			rd.setMessage("删除失败！");
		}
		return rd;
	}
	
	/**
	 * 修改司法所
	 * @param judicialDeptment
	 * @return
	 */
	@RequestMapping("/update")
	public ResultData updateJudicialDeptment(@Valid JudicialDeptment judicialDeptment){ 
		logger.info("修改矫正单位"+judicialDeptment);
		int i=judicialDeptmentService.updateJudicialDeptment(judicialDeptment);
		ResultData rd=new ResultData();
		if(i>0){
			rd.setCode(0);
			rd.setMessage("修改成功！");
		}else{
			rd.setCode(-1);
			rd.setMessage("修改失败！");
		}
		return rd;
	}
	
	/**
	 * 根据id查询司法所
	 * @param judicialDeptment
	 * @return
	 */
	@RequestMapping("/get")
	public ModelAndView getJudicialDeptment(JudicialDeptment judicialDeptment){ 
		logger.info("查询矫正单位"+judicialDeptment);
		judicialDeptment=judicialDeptmentService.getJudicialDeptment(judicialDeptment);
		return new ModelAndView("work-manage/judicial_deptment_edit","jd",judicialDeptment);
	}
	
	/**
	 * 根据司法所名称模糊查询符合条件的司法所
	 * @param pageSize
	 * @param pageNum
	 * @param judicialDeptment
	 * @return
	 */
	@RequestMapping("/querylike")
	public ResultData queryLikeJudicialDeptments(int pageSize, int pageNum,JudicialDeptment judicialDeptment){ 
		logger.info("查询条件"+judicialDeptment);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pageSize", pageSize);
		map.put("pageNum", pageNum);
		map.put("judicialDeptment",judicialDeptment);
		Page page=judicialDeptmentService.queryLikeJudicialDeptments(map);
		List<Object> list=new ArrayList<Object>();
		list.add(page);
		ResultData rd=new ResultData();
		rd.setCode(0);
		rd.setMessage("查询成功！");
		rd.setData(list);
		return rd;
	}
	
	/**
	 * 导出司法所
	 * @param judicialDeptment
	 * @throws IOException 
	 */
	@RequestMapping("/export")
	public void exportJudicialDeptments(JudicialDeptment judicialDeptment,HttpServletResponse response) throws IOException{ 
		logger.info("导出匹配的司法所"+judicialDeptment);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("judicialDeptment",judicialDeptment);
		Workbook wb=judicialDeptmentService.exportJudicialDeptments(map);
        response.setContentType("application/vnd.ms-excel;charset=utf-8");  
        String excelName = "矫正单位";  
        response.setHeader("Content-Disposition","attachment; filename="+new String(excelName.getBytes("utf-8"), "ISO8859-1" )+".xls");  
		wb.write(response.getOutputStream());
	}
	 
	/**
	 * 查询所有司法所
	 * @return
	 */
	@RequestMapping("/queryAll")
	public ResultData queryLikeJudicialDeptments(){ 
		logger.info("查询所有司法所");
		List<Object> list=judicialDeptmentService.listJudicialDeptments();
		ResultData rd=new ResultData();
		rd.setCode(0);
		rd.setMessage("查询成功！");
		rd.setData(list);
		return rd;
	}

	
	/**
	 * 返回页面通用方法
	 * @param page
	 * @return
	 */
	@RequestMapping("/page")
	public ModelAndView page(String page){
		return new ModelAndView(page);
	}
}
